package com.ruoyi.project.module.wtc.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.project.domain.YhAssist;
import com.ruoyi.project.domain.YhEquipment;
import com.ruoyi.project.service.IYhAssistService;
import com.ruoyi.project.service.IYhEquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 设备信息Controller
 *
 * @author ruoyi
 * @date 2023-03-09
 */
@RestController
@RequestMapping("/wtc/equipment")
public class WtcYhEquipmentController extends BaseController {
    @Autowired
    private IYhEquipmentService yhEquipmentService;

    @Autowired
    private IYhAssistService yhAssistService;

    /**
     * 查询设备信息列表
     */
    //@RequiresPermissions("project:equipment:list")
    @GetMapping("/list")
    public TableDataInfo list(YhEquipment yhEquipment) {
        //startPage();
        List<YhEquipment> list = yhEquipmentService.selectYhEquipmentList(yhEquipment);
        return getDataTable(list);
    }

    @GetMapping("/lists")
    public TableDataInfo lists(YhEquipment yhEquipment) {
        List<YhEquipment> list = yhEquipmentService.selectYhEquipmentList(yhEquipment);
        List<YhAssist> listA = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            String instrumentId = list.get(i).getInstrumentId();
            YhAssist yhAssist = new YhAssist();
            yhAssist.setNote1(instrumentId);
            List<YhAssist> assistList = yhAssistService.selectYhAssistList(yhAssist);
            if (assistList.size() != 0) {
                if (!assistList.get(0).getAssistId().equals("")) {
                    assistList.get(0).setNote2("1");
                    listA.addAll(assistList);
                }else{
                    assistList.get(0).setNote2("0");
                    listA.addAll(assistList);
                }
            }else{
                YhAssist assist = new YhAssist();
                assist.setNote2("0");
                listA.add(assist);
            }
        }

        return getDataTable(listA);
    }

    /**
     * 导出设备信息列表
     */
    //@RequiresPermissions("project:equipment:export")
    @Log(title = "设备信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YhEquipment yhEquipment) {
        List<YhEquipment> list = yhEquipmentService.selectYhEquipmentList(yhEquipment);
        ExcelUtil<YhEquipment> util = new ExcelUtil<YhEquipment>(YhEquipment.class);
        util.exportExcel(response, list, "设备信息数据");
    }

    /**
     * 获取设备信息详细信息
     */
    //@RequiresPermissions("project:equipment:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(yhEquipmentService.selectYhEquipmentById(id));
    }

    /**
     * 新增设备信息
     */
    //@RequiresPermissions("project:equipment:add")
    @Log(title = "设备信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YhEquipment yhEquipment) {
        return toAjax(yhEquipmentService.insertYhEquipment(yhEquipment));
    }

    /**
     * 修改设备信息
     */
    //@RequiresPermissions("project:equipment:edit")
    @Log(title = "设备信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YhEquipment yhEquipment) {
        return toAjax(yhEquipmentService.updateYhEquipment(yhEquipment));
    }

    /**
     * 删除设备信息
     */
    //@RequiresPermissions("project:equipment:remove")
    @Log(title = "设备信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(yhEquipmentService.deleteYhEquipmentByIds(ids));
    }
}
